package com.bs.coregamelibrary.config;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.bs.coregamelibrary.watcher.PropertiesWatcher;
import com.jolbox.bonecp.BoneCPConfig;

/**
 *
 * @author MartinLe
 */
public class DatabaseConfig extends PropertiesWatcher {

    private static final String DATABASE_CONFIG_PATH = "config/db.properties";
    private static final String DB_URL = "url";
    private static final String DB_USER_NAME = "username";
    private static final String DB_PASSWORD = "password";
    private static final String DB_PARTITION_COUNT = "partitionCount";
    private static final String DB_MAX_CONNECTION_PER_PARTITION = "maxConnectionsPerPartition";
    private static final String DB_MIN_CONNECTION_PER_PARTITION = "minConnectionsPerPartition";
    private static final String DB_ACQUIRE_INCREMENT = "acquireIncrement";
    private static final String DB_CONNECTION_TIME_OUT = "connectionTimeout";
    private static final String DB_IDLE_MAX_AGE = "idleMaxAge";
    private static final String DB_INIT_SQL = "initSQL";
    private static final String DB_MAX_CONNECTION_AGE = "maxConnectionAge";
    private static final String DB_RELEASE_HELPER_THREAD = "releaseHelperThread";
    private static final String DB_DRIVERS = "drivers";
    private static DatabaseConfig instance;

    public DatabaseConfig(String configFilename) {
        super(configFilename);
    }

    public DatabaseConfig() {
        this(DATABASE_CONFIG_PATH);
    }

    public BoneCPConfig getDatabaseConfig(String database) throws ClassNotFoundException {
        BoneCPConfig boneCPConfig = null;
        Class.forName(getDriver());
        String prefix = "";
        if ((database != null) && (!"".equals(database))) {
            prefix = database + ".";
        }
        boneCPConfig = new BoneCPConfig();
        boneCPConfig.setJdbcUrl(getString(prefix + DB_URL));
        boneCPConfig.setUsername(getString(prefix + DB_USER_NAME));
        boneCPConfig.setPassword(getString(prefix + DB_PASSWORD));
        boneCPConfig.setPartitionCount(getInt(prefix + DB_PARTITION_COUNT));
        boneCPConfig.setMaxConnectionsPerPartition(getInt(prefix + DB_MAX_CONNECTION_PER_PARTITION));
        boneCPConfig.setMinConnectionsPerPartition(getInt(prefix + DB_MIN_CONNECTION_PER_PARTITION));
        boneCPConfig.setAcquireIncrement(getInt(prefix + DB_ACQUIRE_INCREMENT));
        boneCPConfig.setConnectionTimeoutInMs(getLong(prefix + DB_CONNECTION_TIME_OUT));
        boneCPConfig.setIdleMaxAgeInSeconds(getLong(prefix + DB_IDLE_MAX_AGE));

        boneCPConfig.setMaxConnectionAgeInSeconds(getLong(prefix + DB_MAX_CONNECTION_AGE));
        boneCPConfig.setReleaseHelperThreads(getInt(prefix + DB_RELEASE_HELPER_THREAD));
        return boneCPConfig;
    }

    public BoneCPConfig setDatabaseConfig(String database, BoneCPConfig boneCPConfig) {
        try {
            Class.forName(getDriver());
            String prefix = "";
            if ((database != null) && (!"".equals(database))) {
                prefix = database + ".";
            }
            boneCPConfig = getDatabaseConfig(database);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return boneCPConfig;
        }
    }

    public String getDriver() {
        return getString(DB_DRIVERS);
    }
}
